#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QFile>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}
// //////------read/write
void Widget::on_pushButton_2_clicked()
{
    //写文本文件
    QFile file("test.txt");//构造file对象
    if(file.open(QIODevice::WriteOnly)){
//        QByteArray data = ui->textEdit->toPlainText().toLatin1();//QString to ByteArry
//        file.write(data);
//        file.close();
        QTextStream out(&file);
        out.setCodec("UTF-8");
        out<<ui->textEdit->toPlainText();
        file.close();
    }else{
        return;
    }
}

void Widget::on_pushButton_clicked()
{
    QFile file("test.txt");
    if(file.open(QIODevice::ReadOnly)){
       // QString data = file.readAll();
        ui->textEdit->setPlainText(file.readAll());//QByteArray--QString自动转换；
        file.close();
    }else{
        return;
    }
}

void Widget::on_pushButton_4_clicked()
{
    //写文本文件
    QFile file("data.dat");//构造file对象
    if(file.open(QIODevice::WriteOnly)){
        int data = ui->spinBox->value();
        file.write((char*)&data,sizeof (data));
        file.close();
    }else{
        return;
    }
}

void Widget::on_pushButton_3_clicked()
{
    QFile file("data.dat");
    if(file.open(QIODevice::ReadOnly)){
       int data;
       file.read((char*)&data,sizeof(data));
       ui->lineEdit->setText(QString::number(data));
    }else{
        return;
    }
}
//  // //////------------------------------------
//文本流QTextStream
//输出到文件，output
void Widget::on_pushButton_6_clicked()
{
     QFile file("text_Stream.txt");
     if(file.open(QIODevice::WriteOnly)){
        QTextStream out(&file);//通过file构造out
        out.setCodec("UTF-8");
        out << ui->textEdit_2->toPlainText();
        file.close();
     }else{
         return;
     }
}

void Widget::on_pushButton_5_clicked()
{
    QFile file("text_Stream.txt");
    if(file.open(QIODevice::ReadOnly)){
        qDebug()<<"ok";
       QTextStream in(&file);//通过file构造out
       in.setCodec("UTF-8");
        QString data;
       in >> data;
       ui->textEdit_2->setPlainText(data);
       file.close();
    }else{
        return;
    }
}
//dataStream

void Widget::on_pushButton_8_clicked()
{
    QFile file("text_Stream.txt");
    if(file.open(QIODevice::WriteOnly)){
       QTextStream out(&file);//通过file构造out
       //out.setCodec("UTF-8");
       out << ui->spinBox_2->value();
       file.close();
    }else{
        return;
    }
}

void Widget::on_pushButton_7_clicked()
{
    QFile file("text_Stream.txt");
    if(file.open(QIODevice::ReadOnly)){
       QTextStream in(&file);//通过file构造out

       int data;
       in >> data;
       ui->lineEdit_2->setText(QString::number(data));
       file.close();
    }else{
        return;
    }
}
//dataStream
